#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <Ph.h>
#include <Pt.h>
#include <Ap.h>
#include <photon/PxProto.h>

FILE           *log_file;
FILE          *test_file;
struct PxTransCtl* trans;

int
emit_key( unsigned long key_cap )
{
	PhEvent_t event;
	PhKeyEvent_t key;
	PhRect_t rect;
	int retcode;

	memset(&key, 0, sizeof(key));
	key.key_sym = key.key_cap = key_cap;

	memset(&event, 0, sizeof(event));
	event.type = Ph_EV_BUT_PRESS;	
	
	event.type = Ph_EV_KEY;
	event.subtype = 0;

	event.emitter.rid	= Ph_DEV_RID;
	event.collector.rid	= 0;
//	event.flags			= Ph_EVENT_DIRECT;
	event.num_rects = 1;
	rect.ul.x = rect.ul.y = SHRT_MIN;
	rect.lr.x = rect.lr.y = SHRT_MAX;
	event.data_len		= sizeof( key );

	key.key_flags = Pk_KF_Sym_Valid | Pk_KF_Cap_Valid | Pk_KF_Key_Down;
	PhEventEmit( &event, &rect, &key);

	key.key_flags = Pk_KF_Sym_Valid | Pk_KF_Cap_Valid;
	PhEventEmit( &event, &rect, &key);

	sleep(1);
	
	return 0;
}

//pid_t get_conn_pid( PhConnectId_t coid)
//{
//	PhConnectInfo_t buf;
//	struct _psinfo psinfo;
//	
//	if ( 0 < PhGetConnectInfo(coid, &buf) ) {
//		if ( 0 < qnx_psinfo(PROC_PID, buf.pid, &psinfo, 0, NULL) ) {
//			return buf.pid;
//		}
//	}
//	return 0;
//}

//int get_rid_by_pid(pid_t pid, PhRid_t * pids_rids, int rids_size)
int get_rid_info()
{
	int cnt, i, res_cnt, rids_size;
	PhRid_t rids[100];
	char cp866_title[64];
	int taken, made;
	
	rids_size = 100;
	res_cnt = 0;

	
	cnt = PhQueryRids(
		Ph_RIDQUERY_TOWARD, 
		0, 
		0,
		Ph_WINDOW_REGION,
		0,
		Ph_ROOT_RID,
		NULL,
		rids,
		100
	);
	fprintf(log_file, "***************************************\n");

	for (i=cnt-1; i >= 0; i--) {
		PhWindowInfo_t*    window_info;
		PhRegion_t         region;
		PhRect_t           rect;
		char               data[1000];
		
		if ( !PhRegionQuery( rids[i], &region, &rect, (PhRegionDataHdr_t *)&data, sizeof(data) ) ) 
		{	
			window_info = (PhWindowInfo_t *)PhRegionDataFindType( &region, (PhRegionDataHdr_t *)&data, Ph_RDATA_WINDOW);
			if (window_info) {
				PxTranslateFromUTF( trans, window_info->title, 32, &taken, cp866_title, 32, &made);
				fprintf(log_file, "Windows title %s %s %i\n", cp866_title, window_info->win_in_front);
			}
//			fprintf(log_file, "Rid:%d\n", rids[i]);
//			fprintf(log_file, "Sensitive to: %.8x Opaque to: %.8x; type: %d, flags: %.8x; IG: %d; parent: %d ", 
//				region.events_sense, region.events_opaque, region.flags, region.input_group, region.parent );
//			fprintf(log_file, "Located at: {(%d,%d),(%d,%d)}\n", 
//				region.origin.x + rect.ul.x,
//				region.origin.y + rect.ul.y, 
//				region.origin.x + rect.lr.x,
//				region.origin.y + rect.lr.y 
//			);
		}
	}	
	return res_cnt;
}

int
main (int argc, char *argv[])
{
	int i;

	if( NULL == PhAttach( NULL, NULL ) ) {
		fprintf( stderr, "Couldn't attach Photon channel\n");
		exit( EXIT_FAILURE );
	}	

	log_file = fopen("/qnx4/station/log/autotest.log", "w");
	
	trans = PxTranslateSet( NULL, "IBM866" );

	emit_key(Pk_F5);
	emit_key(Pk_F6);

//  Ceni	
	emit_key(Pk_Home);
	emit_key(Pk_Right);
	for (i=0;i<2;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Klienti
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<6;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Protokoli
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<1;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Karti
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<3;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Magazin
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<2;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_F6);
	}
	emit_key(Pk_Down);
	emit_key(Pk_Return);
	sleep(3);
	get_rid_info();
	emit_key(Pk_Return);

	emit_key(Pk_Down);
	emit_key(Pk_Return);
	sleep(3);
	get_rid_info();
	emit_key(Pk_F6);

	for (i=0;i<2;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}

	emit_key(Pk_Down);
	emit_key(Pk_Return);
	sleep(3);
	get_rid_info();
	emit_key(Pk_Escape);
	emit_key(Pk_Return);

	emit_key(Pk_Down);
	emit_key(Pk_Return);
	sleep(3);
	get_rid_info();
	emit_key(Pk_F10);

	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  GSM
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<6;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  TRK
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<1;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Smenni
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<6;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Kassa
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<8;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Mobil
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	for (i=0;i<1;i++) {
		emit_key(Pk_Down);
		emit_key(Pk_Return);
		sleep(3);
		get_rid_info();
		emit_key(Pk_Escape);
	}
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  Printer
	emit_key(Pk_Down);
	emit_key(Pk_Right);
	emit_key(Pk_Down);
	emit_key(Pk_Return);
	sleep(3);
	get_rid_info();
	emit_key(Pk_Left);
	emit_key(Pk_Left);
//  TRK_Pult
	emit_key(Pk_Down);
	emit_key(Pk_Return);



	fclose(log_file);

	return 0;
}
